package system;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;

public class MyCamera {
	
	private static float alpha;
	private static float beta;
	private static float r;
	private static GL10 gl;
	
	private static float s_lookX;
	private static float s_lookY;
	private static float s_lookZ;
	
	private static float s_eyeX;
	private static float s_eyeY;
	private static float s_eyeZ;
	
	private static float s_upX;
	private static float s_upY;
	private static float s_upZ;
	private static float ratio;
	private static float zMin = 0.1f;
	private static float zMax = 100f;
	
	public static void InitCamera(GL10 Gl, float _alpha, float _beta){
		gl = Gl;
		alpha = _alpha;
		beta = _beta;
		
		ratio = Device.GetWidth() / Device.GetHeight();
		
		// Look
		r =  (float) Math.sin(Math.PI*alpha / 180);
		s_lookY = (float) Math.cos(Math.PI*alpha / 180);
		s_lookZ = r * (float) Math.cos(Math.PI*beta / 180);
		s_lookX = r * (float) Math.sin(Math.PI*beta / 180);
		
		// Eye
		s_eyeX = s_eyeY = s_eyeZ = 0;
		
		// Up
		s_upX = s_upZ = 0;
		s_upY = 1;
		
		// Eye & Look setup
		setPerspective();
		GLU.gluLookAt(gl, s_eyeX, s_eyeY, s_eyeZ, s_lookX, s_lookY, s_lookZ, s_upX, s_upY, s_upZ);
	}
	
	private static void setPerspective()
	{
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		GLU.gluPerspective(gl, 40.0f, ratio, zMin, zMax);
	}
	
	public static void rotateAlpha(float delAlpha)
	{
		setPerspective();
		
		alpha += delAlpha; 
		
		if(alpha < 0) alpha += 360;
		else if(alpha >= 360) alpha -= 360;
		
		r = (float) Math.sin(Math.PI*alpha / 180);
		s_lookY = (float) Math.cos(Math.PI*alpha / 180);
		s_lookZ = r * (float) Math.cos(Math.PI*beta / 180);
		s_lookX = r * (float) Math.sin(Math.PI*beta / 180);
		
		GLU.gluLookAt(gl, s_eyeX, s_eyeY, s_eyeZ, s_lookX, s_lookY, s_lookZ, s_upX, s_upY, s_upZ);
	}
	
	
	public static void rotateBeta(float delBeta)
	{
		setPerspective();
		
		beta += delBeta;
		
		if(beta < 0) beta = 360 + beta;
		else if(beta >= 360) beta -= 360;
		
		s_lookZ = r * (float) Math.cos(Math.PI*beta / 180);
		s_lookX = r * (float) Math.sin(Math.PI*beta / 180);
		
		GLU.gluLookAt(gl, s_eyeX, s_eyeY, s_eyeZ, s_lookX, s_lookY, s_lookZ, s_upX, s_upY, s_upZ);	
	}
	
	public static void setLook(float lookX, float lookY, float lookZ)
	{
		setPerspective();
		
		s_lookX = lookX;
		s_lookY = lookY;
		s_lookZ = lookZ;
		
		GLU.gluLookAt(gl, s_eyeX, s_eyeY, s_eyeZ, s_lookX, s_lookY, s_lookZ, s_upX, s_upY, s_upZ);
	}
	
	public static void setEye(float eyeX, float eyeY, float eyeZ)
	{
		setPerspective();
		
		s_eyeX = eyeX;
		s_eyeY = eyeY;
		s_eyeZ = eyeZ;
		
		GLU.gluLookAt(gl, s_eyeX, s_eyeY, s_eyeZ, s_lookX, s_lookY, s_lookZ, s_upX, s_upY, s_upZ);
	}
	
	public static void setUp(float upX, float upY, float upZ)
	{
		setPerspective();
		
		s_upX = upX;
		s_upY = upY;
		s_upZ = upZ;
		
		GLU.gluLookAt(gl, s_eyeX, s_eyeY, s_eyeZ, s_lookX, s_lookY, s_lookZ, s_upX, s_upY, s_upZ);
	}
	
	public static float getAlpha()
	{
		return alpha;
	}
	
	public static float getBeta()
	{
		return beta;
	}
	
	public static float getEyeX()
	{
		return s_eyeX;
	}
	
	public static float getEyeY()
	{
		return s_eyeY;
	}
	
	public static float getEyeZ()
	{
		return s_eyeZ;
	}
	
	public static float getLookX()
	{
		return s_lookX;
	}
	
	public static float getLookY()
	{
		return s_lookY;
	}
	
	public static float getLookZ()
	{
		return s_lookZ;
	}
	
	public static float getUpX()
	{
		return s_upX;
	}
	
	public static float getUpY()
	{
		return s_upY;
	}
	
	public static float getUpZ()
	{
		return s_upZ;
	}

	public static void setzMin(float zMin) {
		MyCamera.zMin = zMin;
	}

	public static float getzMin() {
		return zMin;
	}

	public static void setzMax(float zMax) {
		MyCamera.zMax = zMax;
	}

	public static float getzMax() {
		return zMax;
	}	
}
